<?php
/**
 * Created by PhpStorm.
 * User: tengjs
 * Date: 2020/11/3
 * Time: 7:37
 */

namespace App\Http\Controllers\Api\Research;

use App\Admin\Models\EmployeeModel;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Validator;
use App\Admin\Models\ContentModel;

class ResearchController extends BaseController
{
    /**
     * 科研列表
     *
     * @return array
     */
    public function researchList()
    {
        $file = ['content_id', 'image_url', 'title_en as title', 'content_en as content', 'employee_ids'];
        $nameKey = 'name_en';
        $list = ContentModel::select($file)
            ->where('content_type', '=', 3)
            ->orderBy('order', 'asc')
            ->orderBy('content_id', 'desc')
            ->get();

        $employeeIds = [];
        foreach ($list as $item) {
            $employeeIds = array_merge($employeeIds, $item['employee_ids']);
        }
        $employeeIds = array_filter(array_unique($employeeIds));

        // 查教师列表
        $employees = [];
        if (!empty($employeeIds)) {
            $employees = EmployeeModel::getTeacherList(['employee_ids' => $employeeIds]);
            !empty($employees) && $employees = array_column($employees, null, 'employee_id');
        }

        if (!empty($list)) {
            foreach ($list as &$item) {
                $userList = [];
                if (!empty($item['employee_ids'])) {
                    foreach ($item['employee_ids'] as $employeeId) {
                        if (isset($employees[$employeeId])) {
                            $userList[] = [
                                'user_id' => $employeeId,
                                'user_name' => $employees[$employeeId][$nameKey]
                            ];
                        }
                    }
                }
                $item['user_list'] = $userList;
                unset($item['employee_ids']);
            }
            unset($item);
        }

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => [
                'list' => $list
            ]
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }

    /**
     * 科研详情
     *
     * @return array
     */
    public function researchDetail()
    {
        // 参数校验
        $validator = Validator::make($this->input(), [
            'content_id' => 'required|integer|min:1',
        ], $this->messages(), $this->attributes());

        if ($validator->fails()) {
            return $this->failure($validator->errors()->first());
        }

        $lang = $this->input('lang');
        $nameKey = 'name_zh';
        $file = ['content_id', 'title_zh as title', 'employee_ids', 'content_zh as content'];
        if ($lang == 'en') {
            $file = ['content_id', 'title_en as title', 'employee_ids', 'content_en as content'];
            $nameKey = 'name_en';
        }
        $data = ContentModel::select($file)->where('content_type', '=', 3)
            ->where('content_id', '=', $this->input('content_id', 0))->first();

        // 查教师列表
        $userList = [];
        if (!empty($data['employee_ids'])) {
            $employees = EmployeeModel::getTeacherList($data['employee_ids']);
            !empty($employees) && $employees = array_column($employees, null, 'employee_id');

            foreach ($data['employee_ids'] as $employeeId) {
                if (isset($employees[$employeeId])) {
                    $userList[] = [
                        'user_id' => $employeeId,
                        'user_name' => $employees[$employeeId][$nameKey]
                    ];
                }
            }
        }
        $data['user_list'] = $userList;
        unset($data['employee_ids']);

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => $data
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }

    /**
     * 合作伙伴列表
     *
     * @return array
     */
    public function partnerList()
    {
        $lang = $this->input('lang');
        $file = ['content_id', 'title_zh as title', 'period_zh as url', 'image_url'];
        if ($lang == 'en') {
            $file = ['content_id', 'title_en as title', 'period_en as url', 'image_url'];
        }
        $list = ContentModel::select($file)
            ->where('content_type', '=', 15)
            ->where('title_' . $lang, '<>', '')
            ->orderBy('order', 'asc')
            ->orderBy('content_id', 'desc')
            ->get();

        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['image_url'] = $this->buildImageUrl($item['image_url']);
            }
            unset($item);
        }

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => [
                'list' => $list
            ]
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }
}
